{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# PaddlePaddle-Numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Python被大量应用在数据挖掘和深度学习领域，其中Numpy、Pandas、Matplotlib、PIL等第三方库"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "<img src=\"https://ai-studio-static-online.cdn.bcebos.com/d2ab7dc4c05c42fe85c557a5ed084038822806b23ed544b5bab62517994f5383\">"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "<b>Numpy</b>是Python科学计算库的基础。包含了强大的N维数组对象和向量运算.\n",
    "\n",
    "<b>pandas</b>是建立在numpy基础上的高效数据分析处理库，是Python的重要数据分析库。\n",
    "\n",
    "<b>Matplotlib</b>是一个主要用于绘制二维图形的Python库。用途：绘图、可视化。\n",
    "\n",
    "<b>PIL</b>库是一个具有强大图像处理能力的第三方库。用途：图像处理"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Numpy"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "数组创建：\n",
    "\n",
    "可以使用array函数从常规Python中的列表或元组中创建数组。\n",
    "\n",
    "得到的数组类型是从列表中元素的类型推导出来的。\n",
    "\n",
    "创建数组最简单的办法就是使用array函数。它接受一切序列型的对象（包括其他数组），然后产生一个新的含有传入数据的numpy数组。其中，嵌套序列（比如由一组等长列表组成的列表）将会被转换为一个多维数组"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型: <class 'numpy.ndarray'>\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "#将列表转换为数组\n",
    "array=np.array([[1,2,3],\n",
    "                [4,5,6]])\n",
    "print(\"数据类型:\",type(array))\n",
    "print(array)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型: <class 'numpy.ndarray'>\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "#将元组转换为数组\n",
    "array=np.array(((1,2,3),\n",
    "                (4,5,6)))\n",
    "print(\"数据类型:\",type(array))\n",
    "print(array)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "a=np.array([1,2,3,4])\n",
    "print(type(a))\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Numpy提供了部分函数来创建一些特殊的数组"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#zeros()可以创建指定长度或者形状的全0数组\n",
    "zeroArray=np.zeros((2,3))   #括号里面是它的形状\n",
    "print(zeroArray)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "#ones()可以创建指定长度或者形状的全1数组\n",
    "oneArray=np.ones((3,3))\n",
    "print(oneArray)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.12199579e-314 1.39839523e-311]\n",
      " [5.61258574e-321 3.79442416e-321]]\n"
     ]
    }
   ],
   "source": [
    "#empty()可以创建一个数组，其初始内容是随机的，取决于内存状态，相当于随机数\n",
    "emptyArray=np.empty((2,2))\n",
    "print(emptyArray)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 15 20 25 30]\n"
     ]
    }
   ],
   "source": [
    "#Numpy还提供了一个类似于range的函数，该函数返回数组而非列表\n",
    "array=np.arange(10,31,5)    #start end step\n",
    "print(array)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2.  3.]\n",
      " [ 4.  5.  6.]\n",
      " [ 7.  8.  9.]\n",
      " [10. 11. 12.]]\n"
     ]
    }
   ],
   "source": [
    "#也有一些函数用于查看数组的一些信息\n",
    "\n",
    "#先创建一个数组\n",
    "array = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12.0]])\n",
    "print(array)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "维度: 2\n",
      "形状: (4, 3)\n",
      "元素个数: 12\n",
      "元素类型: float64\n"
     ]
    }
   ],
   "source": [
    "#数组具体信息\n",
    "print(\"维度:\",array.ndim)\n",
    "print(\"形状:\",array.shape)\n",
    "print(\"元素个数:\",array.size)\n",
    "print(\"元素类型:\",array.dtype)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "#看以下demo\n",
    "array1=np.arange(6).reshape(2,3)    #重塑\n",
    "print(array1)\n",
    "\n",
    "array2 = np.array([[1,2,3],[4,5,6]],dtype=np.int64).reshape([3,2])\n",
    "print(array2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数组的计算"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "#数组很重要，因为它可以不借助循环进行批量运算，这种操作叫做矢量化(vectorization)\n",
    "#矩阵的基础运算\n",
    "arr1=np.array([[1,2,3],[4,5,6]])\n",
    "arr2=np.ones([2,3],dtype=np.int64)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(arr1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1]\n",
      " [1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3 4]\n",
      " [5 6 7]]\n",
      "[[0 1 2]\n",
      " [3 4 5]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1. 2. 3.]\n",
      " [4. 5. 6.]]\n",
      "[[ 1  4  9]\n",
      " [16 25 36]]\n"
     ]
    }
   ],
   "source": [
    "#计算\n",
    "print(arr1 + arr2)\n",
    "print(arr1 - arr2)\n",
    "print(arr1 * arr2)\n",
    "print(arr1 / arr2)\n",
    "print(arr1 ** 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 1]\n",
      " [1 1]\n",
      " [1 1]]\n",
      "[[ 6  6]\n",
      " [15 15]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵乘法\n",
    "arr3 = np.array([[1,2,3],[4,5,6]])\n",
    "arr4 = np.ones([3,2],dtype=np.int64)\n",
    "print(arr3)\n",
    "print(arr4)\n",
    "print(np.dot(arr3,arr4))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[ 6 15]\n",
      "6\n",
      "1\n",
      "3.5\n",
      "5\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "#矩阵的其他运算\n",
    "print(arr3)\n",
    "print(np.sum(arr3,axis=1)) #axis=1,每一行求和 axie=0,每一列求和\n",
    "print(np.max(arr3))\n",
    "print(np.min(arr3))\n",
    "print(np.mean(arr3))\n",
    "print(np.argmax(arr3))\n",
    "print(np.argmin(arr3))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(arr3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "source": [
    "arr3_tran=arr3.transpose()  #矩阵转置\n",
    "print(arr3_tran)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "print(arr3.flatten())   #矩阵的一维扁平化"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组的索引与切片"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "arr5=np.arange(0,6).reshape([2,3])\n",
    "print(arr5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5]\n",
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print(arr5[1])\n",
    "print(arr5[1][2])\n",
    "print(arr5[1,2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5]\n",
      "[1 4]\n",
      "[3 4]\n"
     ]
    }
   ],
   "source": [
    "#切片这一块有丢丢不理解\n",
    "print(arr5[1,:])\n",
    "print(arr5[:,1])\n",
    "print(arr5[1,0:2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}